package mhl.view;

import mhl.domain.bill;
import mhl.domain.diningTable;
import mhl.domain.employee;
import mhl.domain.menu;
import mhl.service.billService;
import mhl.service.diningTableService;
import mhl.service.employeeService;
import mhl.service.menuService;
import mhl.utils.Utility;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.UUID;

public class MHLview {

    private boolean loop = true;
    private employeeService employeeservice = new employeeService();
    private diningTableService diningtableservice = new diningTableService();
    private menuService menuservice = new menuService();
    private billService billservice = new billService();

    public static void main(String[] args) {
        MHLview mhLview = new MHLview();
        mhLview.menuPrint();
    }

    public void listMenu() {
        List<menu> menus = menuservice.listMenu();
        System.out.println("菜品编号" + "\t\t" + "菜品名" + "\t\t" + "类别" + "\t\t" + "价格");
        for (menu menu : menus) {
            System.out.println(menu);
        }
    }

    public void listBill() {
        List<bill> bills = billservice.listBill();
        System.out.println("账单号" + "\t\t" + "菜品号" + "\t\t" + "菜品量" + "\t\t" + "金额" + "\t\t" + "桌号" +
                "\t\t" + "日期" + "\t\t" + "状态");
//        id + "\t\t\t"  + menuId + "\t\t\t" + nums + "\t\t" + money + "\t\t" + diningTableId
//                + "\t\t" + billDate.format(formatter) + "\t\t" + status;
        for (bill bill : bills) {
            System.out.println(bill);
        }
    }

    public void orderTable() {
        int diningId = 0;
        while (true) {
            System.out.println("请输入你要预定的座位号（1-50），按-1取消预定");
            diningId = Utility.readInt();
            if (diningId == -1) {
                return;
            }
            diningTable diningTable = diningtableservice.singleDiningTable(diningId);
            //判断当前座位是否存在
            if (diningTable == null) {
                System.out.println(diningId + "座位号不存在,请重新输入");
                continue;
            }
            //判断当前座位是否可用
            if (!("空".equals(diningTable.getStatus()))) {
                System.out.println(diningId + "座位当前处于预定或者就餐中，不可预定");
            } else {
                break;
            }
        }
        System.out.println("请输入是否预定(Y|N)");
        char isConfirm = Utility.readConfirmSelection();
        if (isConfirm == 'N') {
            return;
        }
        System.out.println("请输入预定人姓名：");
        String name = Utility.readString(32);
        System.out.println("请输入预定人电话：");
        String tel = Utility.readString(32);
        int effectRow = diningtableservice.orderDiningTable(name, tel, diningId);
        if (effectRow > 0) {
            System.out.println("预定成功！");
        }
    }


    public void cancelOrder() {
        int diningId = 0;
        while (true) {
            System.out.println("请输入你要取消预定的座位号（1-50），按-1撤回");
            diningId = Utility.readInt();
            if (diningId == -1) {
                return;
            }
            if (diningtableservice.isOrdered(diningId)) {
                break;
            } else {
                System.out.println(diningId + "号座位没有预定，不能取消");
            }
            //判断当前座位是否存在

            //判断当前座位是否可用
        }
        System.out.println("请输入是否取消预定(Y|N)");
        char isConfirm = Utility.readConfirmSelection();
        if (isConfirm == 'N') {
            return;
        }
        int effectRow = diningtableservice.unOrderDiningTable(diningId);
        if (effectRow > 0) {
            System.out.println("取消预定成功！");
        }
    }


    public void orderMenu() {
        int diningId = 0;
        while (true) {
            System.out.println("请输入你要点餐的座位号（1-50）");
            diningId = Utility.readInt();
            diningTable diningTable = diningtableservice.singleDiningTable(diningId);
            //判断当前座位是否存在
            if (diningTable == null) {
                System.out.println(diningId + "座位号不存在,请重新输入");
                continue;
            }
            //判断当前座位是否可用
            if ("已预定".equals(diningTable.getStatus())) {
                System.out.println(diningId + "座位已预定，请输入预定人姓名和电话");
                String name = Utility.readString(32);
                String tel = Utility.readString(11);
                if (diningtableservice.isOrdered(name, tel, diningId)) {
                    System.out.println("当前预定人存在，可以点餐！");
                    break;
                } else {
                    System.out.println("当前预定人不存在，不能点餐！");
                }

            } else {
                break;
            }

        }
        System.out.println("请输入是否点餐(Y|N)");
        char isConfirm = Utility.readConfirmSelection();
        if (isConfirm == 'N') {
            return;
        }
        listMenu();
        System.out.println("请输入菜品号：");
        int menuId = Utility.readInt();
        System.out.println("请输入数量：");
        int nums = Utility.readInt();
        if (billservice.updateBill(menuId, nums, diningId)) {
            System.out.println("点餐完成");
        } else {
            System.out.println("点餐失败");
        }


    }


    public boolean payBill() {
        int diningId = 0;
        while (true) {
            System.out.println("请输入你要结账的座位号（1-50）");
            diningId = Utility.readInt();
            diningTable diningTable = diningtableservice.singleDiningTable(diningId);
            //判断当前座位是否存在
            if (diningTable == null) {
                System.out.println(diningId + "座位号不存在,请重新输入");
                continue;
            }
            //判断当前座位是否可用
            if (!billservice.hasPayBill(diningId)) {
                System.out.println(diningId + "当前没有未支付订单");
            } else {
                break;
            }

        }
        System.out.println("请输入是否结账(Y|N)");
        char isConfirm = Utility.readConfirmSelection();
        if (isConfirm == 'N') {
            return false;
        }
        System.out.println("您当前的未支付订单为：");
        List<bill> bills = billservice.listBill();
        System.out.println("账单号" + "\t\t" + "菜品号" + "\t\t" + "菜品量" + "\t\t" + "金额" + "\t\t" + "桌号" +
                "\t\t" + "日期" + "\t\t" + "状态");
//        id + "\t\t\t"  + menuId + "\t\t\t" + nums + "\t\t" + money + "\t\t" + diningTableId
//                + "\t\t" + billDate.format(formatter) + "\t\t" + status;
        for (bill bill : bills) {
            if ("未结账".equals(bill.getStatus()) && diningId == bill.getDiningTableId()) {
                System.out.println(bill);
            }
        }
        System.out.println("请输入结账方式(微信/支付宝/现金)：");
        String payMode = Utility.readString(32);
        return billservice.updateBill(diningId, payMode) && diningtableservice.updateDiningTableStatusToNull(diningId);

    }


    public void menuPrint() {
        while (loop) {
            System.out.println("===============满汉楼===============");
            System.out.println("\t\t\t\t1 登录系统");
            System.out.println("\t\t\t\t2 退出系统");
            System.out.println("请输入你的选择：");
            int choice = Utility.readInt();
            if (choice == 2) {
                loop = false;
            } else if (choice == 1) {
                System.out.println("请输入员工编号");
                String empId = Utility.readString(60);
                System.out.println("请输入员工密码");
                String pwd = Utility.readString(60);
                employee single = employeeservice.getSingle(empId, pwd);
                if (single == null) {
                    System.out.println("此员工不存在！");
                } else {
                    System.out.println("===============登录成功===============");
                    while (loop) {
                        System.out.println("===============满汉楼二级菜单==========");
                        System.out.println("\t\t1 显示餐桌状态");
                        System.out.println("\t\t2 预定座位");
                        System.out.println("\t\t3 显示菜品");
                        System.out.println("\t\t4 点餐");
                        System.out.println("\t\t5 查看账单");
                        System.out.println("\t\t6 结账");
                        System.out.println("\t\t7 取消预定");
                        System.out.println("\t\t9 退出系统");
                        System.out.println("请输入您的选择：");
                        int key = Utility.readInt();
                        switch (key) {
                            case 1:
                                List<diningTable> diningTables = diningtableservice.listDiningtable();
                                System.out.println("\n餐桌编号\t\t餐桌状态");
                                for (diningTable diningTable : diningTables) {
                                    System.out.println(diningTable);
                                }
                                break;
                            case 2:
                                orderTable();
                                break;
                            case 3:
                                listMenu();
                                break;
                            case 4:
                                orderMenu();
                                break;
                            case 5:
                                listBill();
                                break;
                            case 6:
                                if (payBill()) {
                                    System.out.println("结账成功");
                                } else {
                                    System.out.println("结账失败");
                                }
                                break;
                            case 7:
                                cancelOrder();
                                break;
                            case 9:
                                loop = false;

                        }

                    }

                }

            } else {
                System.out.println("输入错误！");
            }
        }
        System.out.println("欢迎下次光临...");

    }

}
